home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Multi / CircleView.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  101 lines

  1. /*-------------------------------------------------------------------------------
  2.    A simple View subclass that draws a series of circles.  The list of
  3.      circles to be drawn is kept in a Storage object; we just run down
  4.      the list, drawing them.
  5.      
  6.      HISTORY:
  7.          
  8.             10Oct93    DM    New
  9. -------------------------------------------------------------------------------*/
  10.  
  11. #import "CircleView.h"
  12.  
  13. @implementation CircleView
  14.  
  15. - initFrame                                                            // Designated initializer for View
  16.     :(const NXRect*)frameRect;
  17. {
  18.   /*-------------------------------------------------------------------
  19.        The designated initializer for Views.  We take this opportunity
  20.          to initialize our Storage object.
  21.     -------------------------------------------------------------------*/
  22.     
  23.     self = [super initFrame:frameRect];
  24.     
  25.     circleList = [[Storage alloc] initCount:0
  26.                                                                 elementSize:(sizeof(NXPoint) + sizeof(double)) 
  27.                                                                 description:"{dd}d"];
  28.     return self;
  29. }
  30.     
  31. - addCircleAt                                                        // Add a circle to the display list
  32.     :(NXPoint)pPoint                                            // INPUT: the center of the circle
  33.         withRadius:(double)pRadius;                    // INPUT: the radius of the circle
  34. {
  35.   /*-------------------------------------------------------------------
  36.        We get passed in one circle, and it gets added to a display list.
  37.          This lets us redraw and get all the old circles if we want.
  38.          
  39.          CircleList should probably be called something else.  It's a 
  40.          Storage object, not a List.  We should probably also lock-focus
  41.          and draw the new circle directly; this would prevent having to
  42.          draw all the circles in the list just to add one new one.
  43.     -------------------------------------------------------------------*/
  44.     circle    aCircle;
  45.     
  46.     aCircle.center = pPoint;
  47.     aCircle.radius = pRadius;
  48.     
  49.     [circleList addElement:&aCircle];
  50.     
  51.     return self;
  52. }
  53.  
  54. - emptyCircles;                                                    // Remove all the circles from the display list
  55. {
  56.   [circleList empty];
  57.     return self;
  58. }
  59.  
  60. - drawSelf
  61.         :(const NXRect *)rects 
  62.         :(int)rectCount;
  63. {
  64.   /*-------------------------------------------------------------------
  65.        The quintessential View method.  Draws all the circles in the 
  66.          display list.
  67.          
  68.          This is not particularly rapid.  A pswrap would speed things
  69.          up appreciably.  Being smarter about how this is drawn would
  70.          help a lot too.  In fact, this is a pretty bad example of a
  71.          drawSelf: method.
  72.     -------------------------------------------------------------------*/
  73.     int            i;
  74.     circle    *thisCircle;
  75.     
  76.         // lay down the background
  77.         
  78.     PSsetgray(NX_LTGRAY);                                // Wipe out the current contents by filling the view w/ lt. grey
  79.     NXRectFill(&rects[0]);    
  80.     
  81.     PSsetgray(NX_BLACK);                                // Draw a rectangle to frame the view
  82.     NXFrameRect(&rects[0]);
  83.         
  84.         // Loop through the list of Circles, drawing them one-by-one.
  85.         // This would be significantly sped up with a pswrap.
  86.         
  87.     for(i = 0; i < [circleList count]; i++)
  88.         {
  89.             thisCircle = [circleList elementAt:i];
  90.             
  91.             PSnewpath();
  92.             PSarc(thisCircle->center.x, thisCircle->center.y, thisCircle->radius, 0, 360);
  93.             PSstroke();
  94.         }
  95.     
  96.     return self;
  97. }
  98.     
  99.  
  100. @end
  101.